package com.augurit.common.system.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.base.BaseTreeNode;
import com.augurit.common.base.extmodel.base.ITreeNode;
import com.augurit.common.base.extmodel.base.LinkCheckTreeNode;
import com.augurit.common.base.extmodel.inner.AppTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.AcModule;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.web.org.form.AcFunctionForm;
import com.augurit.common.system.web.org.form.AcModuleForm;

public class AcModuleConverter {

	public static AcModuleForm convertToForm(AcModule entity){
		if(entity != null){
			AcModuleForm form = new AcModuleForm();
			
			form.setModuleId(entity.getModuleId());
			form.setModuleCode(entity.getModuleCode());
			form.setModuleName(entity.getModuleName());
			form.setModuleType(entity.getModuleType());
			form.setAppId(entity.getAppId());
			form.setParentModuleId(entity.getParentModuleId());
			form.setModuleLevel(entity.getModuleLevel());
			form.setModuleSeq(entity.getModuleSeq());
			form.setIsLeaf(entity.getIsLeaf());
			form.setSubCount(entity.getSubCount());
			form.setRemark(entity.getRemark());
			
			return form;
		}else
			return null;
	}
	
	public static void convertToEntity(AcModuleForm form, AcModule entity){
		if(form != null && entity != null){
			
			if(form.getModuleId() != null)
				entity.setModuleId(form.getModuleId());
			
			if(form.getModuleCode() != null && form.getModuleCode().trim().length() > 0)
				entity.setModuleCode(form.getModuleCode());
			
			if(form.getModuleName() != null && form.getModuleName().trim().length() > 0)
				entity.setModuleName(form.getModuleName());
			
			if(form.getModuleType() != null && form.getModuleType().trim().length() > 0)
				entity.setModuleType(form.getModuleType());
			
			if(form.getAppId() != null)
				entity.setAppId(form.getAppId());
			
			if(form.getParentModuleId() != null)
				entity.setParentModuleId(form.getParentModuleId());
			
			if(form.getModuleLevel() != null)
				entity.setModuleLevel(form.getModuleLevel());
			
			if(form.getModuleSeq() != null && form.getModuleSeq().trim().length() > 0)
				entity.setModuleSeq(form.getModuleSeq());
			
			if(form.getIsLeaf() != null && form.getIsLeaf().trim().length() > 0)
				entity.setIsLeaf(form.getIsLeaf());
			else
				entity.setIsLeaf(OrgConstant.ZERO);
			
			if(form.getSubCount() != null)
				entity.setSubCount(form.getSubCount());
			
			if(form.getRemark() != null && form.getRemark().trim().length() > 0)
				entity.setRemark(form.getRemark());
		}
	}
	
	public static List<AcModuleForm> convertToFormList(List<AcModule> list){
		return convertToFormList(list,null);
	}
	
	public static List<AcModuleForm> convertToFormList(List<AcModule> list, List<SysCodeForm> moduleTypeList){
		if(list != null && list.size() > 0){
			List<AcModuleForm> result = new ArrayList();
			
			for(AcModule entity : list){
				AcModuleForm form = convertToForm(entity);
				form.setModuleTypeLabel(OrgUtils.getItemName(form.getModuleType(), moduleTypeList));
				result.add(form);
			}
			
			return result;
		}else
			return null;
	}
	
	/**
	 * 将模块form对象转换为ExtTreeNode对象
	 * @param form
	 * @return
	 */
	public static BaseTreeNode convertModuleToExtTreeNode(AcModuleForm form){
		if(form != null){
			BaseTreeNode node = new BaseTreeNode();
			node.setId(AppTreeNode.getKey(AcModule.class.getSimpleName(), form.getModuleId()));
			node.setText(form.getModuleName());
			node.setIconCls(ITreeNode.DEFAULT_NOT_LEAF_TREE_NODE_CLS);
			node.setLeaf(form.getIsLeaf().equals(OrgConstant.LEAF_NODE)? true : false);
			return node;
		}else
			return null;
	}
	
	/**
	 * 将模块form对象转换为ExtCheckTreeNode对象
	 * @param form
	 * @return
	 */
	public static LinkCheckTreeNode convertModuleToCheckTreeNode(AcModuleForm form){
		if(form != null){
			LinkCheckTreeNode node = new LinkCheckTreeNode();
			node.setId(AppTreeNode.getKey(AcModule.class.getSimpleName(), form.getModuleId()));
			node.setText(form.getModuleName());
			node.setIconCls(AppTreeNode.CLS_MODULE);
			node.setLeaf(form.getIsLeaf().equals(OrgConstant.LEAF_NODE)? true : false);
			node.setChecked(false);
			return node;
		}else
			return null;
	}
	
	/**
	 * 将模块form对象转换为AppTreeNode对象
	 * @param form
	 * @return
	 */
	public static AppTreeNode convertModuleToTreeNode(AcModuleForm form){
		if(form != null){
			AppTreeNode node = new AppTreeNode();
			node.setId(AppTreeNode.getKey(AcModule.class.getSimpleName(), form.getModuleId()));
			node.setText(form.getModuleName());
			node.setQtip(form.getModuleCode());
			node.setIconCls(AppTreeNode.CLS_MODULE);
			node.setLeaf(false);
			return node;
		}else
			return null;
	}
	
	/**
	 * 将模块Form对象的子模块列表、功能列表转换为ExtTreeNode列表
	 * @param form
	 * @return
	 */
	public static List<AppTreeNode> convertChildListToTreeNodeList(AcModuleForm form){
		List<AppTreeNode> result = null;
		if(form != null){
			result = new ArrayList<AppTreeNode>();
			if(form.getChildren() != null && form.getChildren().size() > 0){
				for(AcModuleForm module:form.getChildren()){
					result.add( convertModuleToTreeNode(module) );
				}
			}
			
			if(form.getFunctions() != null && form.getFunctions().size() > 0){
				for(AcFunctionForm func:form.getFunctions()){
					result.add(AcFunctionConverter.convertFuncToTreeNode(func) );
				}
			}
			
		}
		return result;
	}
}
